package com.yinhai.msg.email.rest;

import com.yinhai.msg.email.entity.MsgEmailPo;
import com.yinhai.msg.email.service.read.MsgEmailReadService;
import com.yinhai.msg.email.service.write.MsgEmailWriteService;
import com.yinhai.msg.email.tool.SendMailUtil;
import com.yinhai.ta404.core.exception.AppException;
import com.yinhai.ta404.core.restservice.BaseRestService;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import org.simplejavamail.MailException;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 邮箱配置表(MsgEmail)表控制层
 *
 * @author makejava
 * @since 2019-03-14 10:25:33
 */
@RestService({"msgEmail/msgEmailRestService"})
@Validated
/*@RestController
@RequestMapping("msgEmail/msgEmailRestService")*/
public class MsgEmailRestService extends BaseRestService {
    /**
     * 服务对象
     */
    @Resource
    private MsgEmailReadService msgEmailReadService;
    @Resource
    private MsgEmailWriteService msgEmailWriteService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public void selectOne(String id) {
        this.msgEmailReadService.queryById(id);
    }

    /**
     * 通过条件查询数据
     *
     * @return 数据
     */
    @PostMapping({"selectMsgEmail"})
    public void selectMsgEmail(MsgEmailPo msgEmailPo, PageParam pageParam) {
//        List<MsgEmailPo> msgemail = this.msgEmailReadService.queryAll(msgEmailPo,pageParam);
//        this.setData("msgemail", msgemail);
        this.setPageBean(this.msgEmailReadService.queryAll(msgEmailPo,pageParam));
    }

    /**
     * 新增数据
     */
    @PostMapping("insertMsgEmail")
    public void insertMsgEmail(MsgEmailPo msgEmailPo) {
        MsgEmailPo msgEmailPo1=getEmailByEmailAddress(msgEmailPo.getEmailaddress());
        if(msgEmailPo1==null){
            Date date = new Date();
            msgEmailPo.setId(UUID.randomUUID().toString());
            msgEmailPo.setCreatetime(date);
            msgEmailPo.setUpdatetime(date);
            msgEmailPo.setCreateuser(getCurUserAccount().getUsername());
            if(msgEmailPo.getSendssl().equals("1")){
                msgEmailPo.setSendtls("0");
            }else{
                msgEmailPo.setSendtls("1");
            }
            try{
                this.msgEmailWriteService.insert(msgEmailPo);
                setSuccess(true);
            }
            catch (MailException e){
                e.printStackTrace();
                throw new AppException("邮箱验证失败!请核对后重试！");
            }
        }else{
            throw new AppException("该邮箱账号已存在！");
        }

    }

    /**
     * 修改数据
     */
    @PostMapping("updateMsgEmail")
    public void updateMsgEmail(MsgEmailPo msgEmailPo) {
        MsgEmailPo msgEmailPo1=getEmailByEmailAddress(msgEmailPo.getEmailaddress());
        if(msgEmailPo1!=null && (!msgEmailPo1.getId().equals(msgEmailPo.getId()))){
            throw new AppException("新修改的邮箱账号已存在！");
        }else {
            Date date = new Date();
            msgEmailPo.setUpdatetime(date);
            if(msgEmailPo.getSendssl().equals("1")){
                msgEmailPo.setSendtls( "0");
            }else{
                msgEmailPo.setSendtls( "1");
            }
            try {
                this.msgEmailWriteService.update(msgEmailPo);
                setSuccess(true);
            }catch (MailException e){
                e.printStackTrace();
                throw new AppException("邮箱验证失败!请核对后重试！");
            }
        }

    }

    /**
     * 删除数据
     */
    @PostMapping("deleteMsgEmail")
    public void deleteMsgEmail(String emailId) {
        String[] ids = emailId.split(",");
        for (String id : ids) {
            this.msgEmailWriteService.deleteById(id);
        }
        setSuccess(true);
    }

    /**
     * 发送邮件
     */
    @PostMapping("sendMsgEmail")
    public void sendMsgEmail(MsgEmailPo msgEmailPo , HttpServletRequest request) throws MessagingException {
        MsgEmailPo msgemail = this.msgEmailReadService.queryById(msgEmailPo.getId());
        Map map = msgemail.toMap();
        String outbox = request.getParameter("outbox");
        map.put("outbox",outbox);
        String inbox = request.getParameter("inbox");
        map.put("inbox",inbox);
        String sendcontent = request.getParameter("sendcontent");
        map.put("sendcontent",sendcontent);
        String timesend = request.getParameter("timesend");
        map.put("timesend",timesend);
        String resend = request.getParameter("resend");
        map.put("resend",resend);
        String sendtitle = request.getParameter("sendtitle");
        map.put("sendtitle",sendtitle);
        SendMailUtil.SendMail(map);
        setSuccess(true);
    }

    /**
     * 根据邮箱账号查询邮箱，用于账号查重
     * @param emailAddress
     * @return
     */
    public MsgEmailPo getEmailByEmailAddress(String emailAddress){
        MsgEmailPo msgEmailPo=msgEmailReadService.getEmailByEmailAddress(emailAddress);
        return msgEmailPo;
    }
}

